package com.hejia.alauda.persistence.mybatis;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hejia.alauda.entity.User;
import com.hejia.alauda.persistence.BaseMapper;

public interface UserMapper extends BaseMapper<User> {
	/**
	 * 总用户数
	 * 
	 * @param
	 * 
	 * @return
	 */
	public int numberOfUser();

	/**
	 * 单日用户数
	 * 
	 * @param regdate
	 *            日期(yyyy-mm-dd)
	 * @return
	 */
	public int dayNumOfUser(String regdate);

	@Update("update `user` set external=#{external} where id=#{id}")
	int updateUserExternal(@Param("external") String external,
			@Param("id") Long id);

	@Select("select * from `user`")
	@ResultType(User.class)
	List<User> findAllUsers();
	
	@Select("select u.* from user `u`, financing f where f.user_id=u.id and product_id=#{productId} group by f.user_id;")
	@ResultType(User.class)
	List<User> findUsersByProduct(@Param("productId")Long productId);

	/**
	 * 注册用户数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Select("select DATE_FORMAT(createDate,'%Y-%m-%d') as xdate,count(1) as num from `user` a where str_to_date(createdate,'%Y-%m-%d')>=#{0} and str_to_date(createdate,'%Y-%m-%d')<=#{1} group by DATE_FORMAT(createDate,'%Y-%m-%d')")
	public List<Map<String, Object>> countRegister(String startDate,
			String endDate);

	/**
	 * 认证用户（绑卡）
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Select("select DATE_FORMAT(createDate,'%Y-%m-%d') as xdate,count(1) as num from `user` a where str_to_date(createdate,'%Y-%m-%d')>=#{0} and str_to_date(createdate,'%Y-%m-%d')<=#{1} and idCard!='' group by DATE_FORMAT(createDate,'%Y-%m-%d')")
	public List<Map<String, Object>> countCertifyuser(String startDate,
			String endDate);

	/**
	 * 注册用户数总和
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Select("select DATE_FORMAT(createDate,'%Y-%m-%d') as xdate,count(1) as num from `user` a group by DATE_FORMAT(createDate,'%Y-%m-%d')")
	public List<Map<String, Object>> allcountRegister(String startDate,
			String endDate);

	/**
	 * 认证用户（绑卡）总和
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Select("select DATE_FORMAT(createDate,'%Y-%m-%d') as xdate,count(1) as num from `user` a where idCard!='' group by DATE_FORMAT(createDate,'%Y-%m-%d')")
	public List<Map<String, Object>> allcountCertifyuser(String startDate,
			String endDate);

	/**
	 * 查询新注册用户
	 * 
	 * @param map
	 * @return
	 */
	List<User> selectNewUsers(Map<String, Object> map);

	/**
	 * 查询新注册用户条数
	 * 
	 * @param map
	 * @return
	 */
	int selectNewUsersCount(Map<String, Object> map);

	/**
	 * 查询已跟进用户
	 * 
	 * @param map
	 * @return
	 */
	List<User> selectFollowSleepingUsers(Map<String, Object> map);

	/**
	 * 查询已跟进用户条数
	 * 
	 * @param map
	 * @return
	 */
	int selectFollowSleepingUsersCount(Map<String, Object> map);
	
	int countUserIncomeMoreThan(BigDecimal amount);
}
